import request from '@/utils/request.js'

const prev = '/asset/inventory/';

export default {

  // 创建Websocket监听
  createWebsocket(path, onMessage) {
    const socket = new WebSocket(`ws://localhost:8085/${path}`);

    // Set up event listeners
    socket.onopen = (event) => {
      console.log('RFID WebSocket connection opened.');
    };

    socket.onmessage = (event) => {
      const data = event.data;
      onMessage(data);
      const list = rfidList.value;
      if (list.indexOf(data) === -1) {
        list.push(data);
      }
    };

    socket.onerror = (error) => {
      console.error('WebSocket Error:', error);
    };

    socket.onclose = (event) => {
      console.log('WebSocket connection closed.');
    };

    return socket;
  },

  // 盘点
  inventory: function() {
    return request({
      url: `${prev}inventory`,
      method: 'put'
    })
  },
  stopInventory: function() {
    return request({
      url: `${prev}/stopInventory`,
      method: 'put'
    })
  },

}

